package cz.fel.via.timemanagement;

import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.domain.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class StudentActivity extends Activity {

	private final static int ONLY_ME_NOTES = 15;
	private final static int ONLY_ME_EXAMS = 25;
	String username;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.student);

		username = (String) TimeManagement.getKey("username");
		if (username == null) {
			username = TimeManagement.getUser();
		}
		Fasade fasade = TimeManagement.getFasade();
		Student student = fasade.find(username, Student.class);

		// TODO kdyz je blbe username ;)

		TextView name = (TextView) findViewById(R.id.studentName);
		name.setText(student.getFirstName()+" "+student.getLastName());
		name.setGravity(Gravity.CENTER_HORIZONTAL);

		Button coursesButton = (Button) findViewById(R.id.coursesButton);
		coursesButton.setGravity(Gravity.CENTER_HORIZONTAL);
		
		TextView login = (TextView) findViewById(R.id.username);
		login.setText(student.getUsername());

		TextView email = (TextView) findViewById(R.id.email);
		email.setText(student.getEmail());

		TextView studyform = (TextView) findViewById(R.id.studyform);
		switch (student.getStudyForm()) {
		case FULLTIME:
			studyform.setText(R.string.form_fullTime);
			break;
		case PARTTIME:
			studyform.setText(R.string.form_partTime);
			break;
		case DISTANCE:
			studyform.setText(R.string.form_distance);
			break;
		case SELF_PAYER:
			studyform.setText(R.string.form_selfpay);
			break;
		case UNDEFINED:
		default:
			studyform.setText(R.string.form_undef);
			break;
		}

		TextView studyprogramme = (TextView) findViewById(R.id.studyprogramme);
		studyprogramme.setText(student.getProgramme().getName());

		TextView studystate = (TextView) findViewById(R.id.studystate);
		switch (student.getStudyState()) {
		case ACTIVE:
			studystate.setText(R.string.state_active);
			break;
		case CLOSED:
			studystate.setText(R.string.state_closed);
			break;
		case INTERRUPTED:
			studystate.setText(R.string.state_interrupted);
		break;
		case UNDEFINED:
			default:
				studystate.setText(R.string.state_undef);
		}

	}

	public void notes(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, NoteActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_NOTES);
		}
	}

	public void schedule(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, ScheduleActivity.class);
		startActivity(intent);
	}

	public void exams(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, ExamActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_EXAMS);
		}
	}
	
	public void courses(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, CoursesActivity.class);
		startActivity(intent);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ONLY_ME_NOTES:
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
			alertbox.setMessage(R.string.onlyMeNotes);
			alertbox.setNeutralButton("Ok", null);
			return alertbox.show();
		case ONLY_ME_EXAMS:
			AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
			alertbox1.setMessage(R.string.onlyMeExams);
			alertbox1.setNeutralButton("Ok", null);
			return alertbox1.show();
		}
		return null;
	}
}
